// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.jxw.cloudpen.web.microsoft;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.*;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.CoreUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * Sample demonstrates how to get chat completions for the provided chat messages.
 * Completions support a wide variety of tasks and generate text that continues from or "completes" provided
 * prompt data.
 */
public class GetChatCompletionsStreamAsyncSample {

    /**
     * Runs the sample algorithm and demonstrates how to get chat completions for the provided chat messages.
     * Completions support a wide variety of tasks and generate text that continues from or "completes" provided
     * prompt data.
     *
     * @param args Unused. Arguments to the program.
     */
    public static void main(String[] args) throws InterruptedException {
//        String azureOpenaiKey = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_KEY");
//        String endpoint = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_ENDPOINT");
//        String deploymentOrModelId = "{azure-open-ai-deployment-model-id}";


        String endpoint = "https://99266-m60gkcsf-francecentral.cognitiveservices.azure.com"; // 替换为你的终结点
        String azureOpenaiKey = "CrTPvRlLgZyQmMUQ4MAOI1NBEioGFXnW5Vzv20Equag5UOYGbJS8JQQJ99BAAC5T7U2XJ3w3AAAAACOGooZb"; // 替换为你的 API 密钥
        String deploymentOrModelId = "gpt-4o-mini";



        OpenAIAsyncClient client = new OpenAIClientBuilder()
            .endpoint(endpoint)
            .credential(new AzureKeyCredential(azureOpenaiKey))
            .buildAsyncClient();

        List<ChatRequestMessage> chatMessages = new ArrayList<>();
//        chatMessages.add(new ChatRequestSystemMessage("You are a helpful assistant. You will talk like a pirate."));
//        chatMessages.add(new ChatRequestUserMessage("Can you help me?"));
//        chatMessages.add(new ChatRequestAssistantMessage("Of course, me hearty! What can I do for ye?"));
//        chatMessages.add(new ChatRequestUserMessage("What's the best way to train a parrot?"));

        chatMessages.add(new ChatRequestUserMessage("背一首李白的诗"));
        chatMessages.add(new ChatRequestAssistantMessage("青青河畔草，绵绵思远道。远道不可思，归去来兮辞。"));
        chatMessages.add(new ChatRequestUserMessage("再来两首"));


        client.getChatCompletionsStream(deploymentOrModelId, new ChatCompletionsOptions(chatMessages))
            .map(chatCompletions -> {
                /* The delta is the message content for a streaming response.
                 * Subsequence of streaming delta will be like:
                 * "delta": {
                 *     "role": "assistant"
                 * },
                 * "delta": {
                 *     "content": "Why"
                 * },
                 * "delta": {
                 *     "content": " don"
                 * },
                 * "delta": {
                 *     "content": "'t"
                 * }
                 */

                if (CoreUtils.isNullOrEmpty(chatCompletions.getChoices())) {
                    return "";
                }

                ChatResponseMessage delta = chatCompletions.getChoices().get(0).getDelta();

                if (delta.getRole() != null) {
                    System.out.println("Role = " + delta.getRole());
                }
                return delta.getContent() == null ? "" : delta.getContent();
            })
//            .subscribe(
//                System.out::print,
//                error -> System.err.println("There was an error getting chat completions." + error),
//                () -> System.out.println("Completed called getChatCompletionsStream."));
            .subscribe(
            new TestConsumer(),
            new ErrorConsumer(),
            new CloseConsumer());

        // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep
        // the thread so the program does not end before the send operation is complete. Using .block() instead of
        // .subscribe() will turn this into a synchronous call.
        TimeUnit.SECONDS.sleep(10);
        System.out.println("bbbbbbbbbbbbbbb");
    }

    static class TestConsumer implements Consumer {


        @Override
        public void accept(Object o) {
            System.out.print(o);
        }
    }

    static class ErrorConsumer implements Consumer {


        @Override
        public void accept(Object error) {
            System.err.println("There was an error getting chat completions." + error);
        }
    }


    static class CloseConsumer implements Runnable {
        @Override
        public void run() {
            System.out.println("Completed called getChatCompletionsStream.");
        }
    }
}
